//显示交易信息
const express = require('express');
const pool = require('../pool.js');
const AlipayFormData = require('alipay-sdk/lib/form').default;
const alipaySdk = require('../utils/alipayUtil')
const r = express.Router();
const axios = require("axios");
const jwt = require("../jwt");

//显示交易信息
r.post('/tran',(req,res)=>{
  pool.query('select * from user_deal where user_id = ?',[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        message: "修改成功",
        code: 200,
        result:result
      })
    }else{
      res.send({
        message: "修改失败",
        code: 201,
      })
    }
  })
})

//修改密码
r.post('/update',(req,res)=>{
  pool.query('update user_information set wpwd=? where wid=?',[
    req.body.wpwd,
    req.body.wid,
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "修改成功",
        code: 200,
      })
    }else{
      res.send({
        message: "修改失败",
        code: 201,
      })
    }
  })
})
//插入用户爱好表
r.post('/reg1',(req,res)=>{
  console.log(req.body.id);
  console.log(req.body.wrank);
  console.log(req.body.wpoint);
  pool.query('insert into user_rights(user_rights_id,wrank,wpoint) value (?,?,?)',[
    req.body.id,
    req.body.wrank,
    req.body.wpoint,
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "插入成功",
        code: 200,
      })
    }else{
      res.send({
        message: "插入失败",
        code: 201,
      })
    }
  })
})
//插入用户客房爱好
r.post('/reg2',(req,res)=>{
  console.log(req.body.id);
  console.log(req.body.sleep_bed);
  console.log(req.body.sleep_smoke);
  console.log(req.body.sleep_style);
  console.log(req.body.food_style);
  pool.query('insert into user_hobby(user_hobby_id,sleep_bed,sleep_smoke,sleep_style,food_style) value (?,?,?,?,?)',[
    req.body.id,
    req.body.sleep_bed,
    req.body.sleep_smoke,
    req.body.sleep_style,
    req.body.food_style
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "插入成功",
        code: 200,
      })
    }else{
      res.send({
        message: "插入失败",
        code: 201,
      })
    }
  })
})
//
r.get('/pro',(req,res)=>{
  console.log(1);
  res.send({title:'Express'});
})

//查看电话号码是否重复
r.post('/check',(req,res)=>{
  pool.query('select * from user_information where wemail = ? ',[req.body.wemail],(err,result)=>{
    if(err) throw err;
    if (result.length) {
      res.send({
        message: "邮箱已注册",
        code: 200,
      })
    } else {
      res.send({
        message: "邮箱未注册",
        code: 201,
      })
    }
  })
})

//注册
r.post('/register',(req,res)=>{
  pool.query('insert into user_information(wname,wbirthday,wemail,wpwd,wdistrict,wphone,wsex) value (?,?,?,?,?,?,?)',[
    req.body.wname,
    req.body.wbirthday,
    req.body.wemail,
    req.body.wpwd,
    req.body.wdistrict,
    req.body.wphone,
    req.body.wsex
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "注册成功",
        code: 200,
      })
    }else{
      res.send({
        message: "注册失败",
        code: 201,
      })
    }
  })
})
//登录
r.post('/login', (req, res) => {
  pool.query('select * from user_information where wemail = ? and wpwd = ?', [req.body.wemail, req.body.wpwd], (err, result) => {
    if (err) throw err;
    if (result.length) {
      res.send({
        message: "登录成功",
        code: 200,
        result:result,
        token: jwt.generateToken(result[0])
      })
    } else {
      res.send({
        message: "登录失败",
        code: 201,
        result:result
      })
    }
  })
})

// 我的账户
r.post('/add', (req, res) => {
  console.log(12);
  pool.query('select * from user_information where wemail = ? and wpwd = ?', [req.body.wemail, req.body.wpwd], (err, result) => {
    if (err) throw err;
    if (result.length) {
      res.send({
        message: "查询成功",
        code: 200,
        result
      })
    } else {
      res.send({
        message: "查询失败",
        code: 201,
        result
      })
    }
  })
})
// 交易记录
r.post('/tran', (req, res) => {
  pool.query('select * from user_deal where user_deal = ?', [req.body.user_deal], (err, result) => {
    if (err) throw err;
    if (result.length) {
      res.send({
        message: "查询成功",
        code: 200,
        result
      })
    } else {
      res.send({
        message: "查询失败",
        code: 201,
        result
      })
    }
  })
})
// 我的资料
r.post('/data', (req, res) => {
  console.log('我的资料')
  pool.query('select * from user_information where wid = ?', [req.body.wid], (err, result) => {
    if (err) throw err;
    if (result.length) {
      res.send({
        message: "查询成功",
        code: 200,
        result
      })
    } else {
      res.send({
        message: "查询失败",
        code: 201,
        result
      })
    }
  })
})
// 邮件地址
r.post('/addr',(req,res)=>{
  pool.query('insert into user_email_information value (?,?,?,?)',[
    req.body.user_email_id,
    req.body.user_street,
    req.body.user_home,
    req.body.user_zone
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "插入成功",
        code: 200,
      })
    }else{
      res.send({
        message: "插入失败",
        code: 201,
      })
    }
  }
  )
})
//新增电话号码
r.post('/pth',(req,res)=>{
  pool.query('insert into user_phone_information value (?,?,?)',[
    req.body.user_phone_id,
    req.body.user_phone,
    req.body.user_name,
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "插入成功",
        code: 200,
      })
    }else{
      res.send({
        message: "插入失败",
        code: 201,
      })
    }
  })
})
// 用户客房安排喜好
r.post('/hobby',(req,res)=>{
  pool.query('insert into user_hobby(user_hobby_id,sleep_bed,sleep_smoke,sleep_style) value (?,?,?,?)',[
    req.body.user_hobby_id,
    req.body.sleep_bed,
    req.body.sleep_smoke,
    req.body.sleep_style],
    (err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        message: "插入成功",
        code: 200,
      })
    }else{
      res.send({
        message: "插入失败",
        code: 201,
      })
    }
  })
}),
//add首页查询显示
r.get('/content', (req, res) => {
  // var name=req.query.name
  // var phone=req.query.phone
  // var district=req.query.district
  // var avatar=req.query.avatar
  var id=req.query.id
  let sql = 'SELECT * from user_information where wid=?';
  // name,phone,district,avatar,   wname,wphone,wdistrict,wavatar
  pool.query(sql,[id], (error, results) => {
    if (error) throw error;
    if(results.length){
      res.send({
      message: 'ok',
      code: 200,
      results: results
    })
    }else{
      res.send({
        message: 'no',
        code: 201,
        results: results
      })
    }

  })
}),
// 客房喜好查询
r.get('/hobby', (req, res) => {
  var id = req.query.id
  let sql = 'SELECT sleep_bed,sleep_smoke,sleep_style from user_hobby where user_hobby_id=?';
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    })
  })
})
// 客房喜好更改
r.get('/sethobby', (req, res) => {
  // let obj=req.query.obj
  var radios = req.query.radios
  var radios1 = req.query.radios1
  var radios2 = req.query.radios2
  var id = req.query.id
  let sql = 'update user_hobby set sleep_bed=?,sleep_smoke=?, sleep_style=? where user_hobby_id=?;';
  pool.query(sql, [radios, radios1, radios2, id], (error, results) => {
    if (error) throw error;
    if (results.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        results: results
      })
    }else{
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})
// 用户修改邮寄地址
r.get('/setadd', (req, res) => {
  // let obj=req.query.obj
  var street = req.query.gjs;
  var home = req.query.sfs;
  var zone = req.query.xss;
  var id = req.query.id;
  var xq = req.query.xq;
  // console.log(street)
  // console.log(typeof home)
  // console.log(zone)
  let sql = 'update user_email_information set user_street=?,user_home=?,user_zone=?,user_shequ=?where user_email_id=?;';
  pool.query(sql, [street,home,zone,xq,id], (error, results) => {
    if (error) throw error;
    if (results.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        results: results
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})

// 查询用户邮寄地址
r.get('/seladd', (req, res) => {
  var id = req.query.id
  let sql = 'SELECT * from user_email_information where user_email_id=?';
  // name,phone,district,avatar,   wname,wphone,wdistrict,wavatar
  pool.query(sql, [id], (error, results) => {
    console.log(results)
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    })
  })
})

// 给用户插入邮寄地址
r.get('/inadd', (req, res) => {
  // let obj=req.query.obj
  var gjs = req.query.gjs;
  var sfs = req.query.sfs;
  var xss = req.query.xss;
  var id = req.query.id;
  var xq = req.query.xq;
  // console.log(street)
  // console.log(home)
  // console.log(zone)
  let sql = 'insert into user_email_information(user_email_id,user_street,user_home,user_zone,user_shequ) value(?,?,?,?,?)';
  pool.query(sql, [id, gjs, sfs, xss,xq], (error, results) => {
    if (error) throw error;
    if (results.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        results: results
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})


// 用户删除邮寄地址
r.get('/deadd', (req, res) => {
  // let obj=req.query.obj
  // var gjs = req.query.gjs
  // var sfs = req.query.sfs
  // var xss = req.query.xss
  var id = req.query.id
  let sql = 'delete from user_email_information where user_email_id=?';
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    if (results.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        results: results
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})
//
r.post('/api/pcpay', (req, res) => {
  let orderId=req.body.orderId;
  let price = req.body.price;
  let detail = req.body.detail;
  let title = req.body.title;
  console.log(1);
  // * 添加购物车支付支付宝 */
  // 调用 setMethod 并传入 get，会返回可以跳转到支付页面的 url
  const formData = new AlipayFormData();
  formData.setMethod('get'); 
  formData.addField('returnUrl', 'http://localhost:8080/add');//加在
  // 通过 addField 增加参数
  // 在用户支付完成之后，支付宝服务器会根据传入的 notify_url，以 POST 请求的形式将支付结果作为参数通知到商户系统。
  // formData.addField('returnUrl', 'https://www.baidu.com'); // 支付成功回调地址，必须为可以直接访问的地址，不能带参数
  formData.addField('bizContent', {
      outTradeNo: orderId, // 商户订单号,64个字符以内、可包含字母、数字、下划线,且不能重复
      productCode: 'FAST_INSTANT_TRADE_PAY', // 销售产品码，与支付宝签约的产品码名称,仅支持FAST_INSTANT_TRADE_PAY
      totalAmount: price, // 订单总金额，单位为元，精确到小数点后两位
      subject: title, // 订单标题
      body: detail, // 订单描述

  });
//  这里才有效果,不是加在bizContent 里面
  // 如果需要支付后跳转到商户界面，可以增加属性"returnUrl"
  const result =  alipaySdk.exec(  // result 为可以跳转到支付链接的 url
      'alipay.trade.page.pay', // 统一收单下单并支付页面接口
      {}, // api 请求的参数（包含“公共请求参数”和“业务参数”）
      { formData: formData },
  );
  result.then((resp)=>{
      res.send(
          {
              "success": true,
              "message": "success",
              "code": 200,
              "timestamp": (new Date()).getTime(),
              "result": resp
          }
      )
  })

});

//获取订单状态
r.post(`/api/queryOrder`,(req,res)=>{
    let out_trade_no = req.body.out_trade_no;
    let trade_no = req.body.trade_no;
    const formData = new AlipayFormData();
    formData.setMethod('get');
    formData.addField('bizContent', {
      out_trade_no,
      trade_no
  });
  const result =  alipaySdk.exec(  // result 为可以跳转到支付链接的 url
    'alipay.trade.query', // 统一收单下单并支付页面接口
    {}, // api 请求的参数（包含“公共请求参数”和“业务参数”）
    { formData: formData },
);
    result.then(resData=>{
      axios({
        url:resData,
        method:"get"
      }).then(data=>{
        let r = data.data.alipay_trade_query_response;
        if(r.code === '10000'){
          switch(r.trade_status){
              case 'WAIT_BUYER_PAY':
                res.send({
                  success:true,
                  code:200,
                  msg:"支付宝有交易记录,没付款"
                })
                break;

              case 'TRADE_FINISHED':
                  res.send({
                    success:true,
                    code:200,
                    msg:"交易完成,不可退款"
                  })
                  break;


              case 'TRADE_CLOSED':
                    res.send({
                      success:true,
                      code:200,
                      msg:"未付款交易超时关闭,或支付完成后全额或退款"
                    })
                  break;


              case 'TRADE_SUCCESS':
                    res.send({
                      success:true,
                      code:200,
                      msg:"交易成功"
                    })
                    break;
          }
          }else if(r.code === '4000'){
            res.send('交易不存在')
        }
      }).catch(err=>{
        res.send({
          msg:"查询失败",
          err
        })
      })
    })
})

// 用户修改头像
r.get('/setimg', (req, res) => {
  var ljs=req.query.lj;
  var ids = req.query.id;
  let sql = 'update user_information set wavatar=? where wid=?;';
  pool.query(sql, [ljs, ids], (error, results) => {
    console.log(results)
    if (error) throw error;
    if (results.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        results: results
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  }
  )
})

//修改用户积分
r.post('/score',(req,res)=>{
  pool.query(`update user_rights set wpoint= ? where user_rights_id=?`,[req.body.score,req.body.wid],(err,result)=>{
    console.log(result)
    if (err) throw error;
    if (result.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        result: result
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})
//查找积分
r.post('/right',(req,res)=>{
  pool.query(`select * from user_rights where user_rights_id = ?`,[req.body.wid],(err,result)=>{
    console.log(result);
    if (err) throw error;
    if (result.length) {
      res.send({
        message: 'ok',
        code: 200,
        result: result
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})

//修改用户等级
r.post('/rank',(req,res)=>{
  pool.query('update user_rights set wrank= ? where user_rights_id=?',[req.body.rank,req.body.id],(err,result)=>{
    console.log(result);
    if (err) throw error;
    if (result.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        result: result
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  }
  )
})
//用户更改电子邮箱
r.post('/setemail',(req,res)=>{
  console.log(req.body.email);
  console.log(req.body.uid)
  pool.query('update user_information set wemail=? where wid=?',[req.body.email,req.body.uid],(err,result)=>{
    console.log(result);
    if (err) throw err;
    if (result.affectedRows) {
      res.send({
        message: 'ok',
        code: 200,
        result: result
      })
    } else {
      res.send({
        message: '不好使',
        code: 201,
      })
    }
  })
})
// 用户更改电子手机号
r.get('/setphone',(req,res)=>{
    var id = req.query.id;
    let phone =req.query.phone
  let sql = 'update user_information set wphone=? where wid=?';
    pool.query(sql,[phone,id],(error,results)=>{
      if (error) throw error;
      if (results.affectedRows){
        res.send({
          message: 'ok',
          code: 200,
          results: results
        })
      }else{
        res.send({
          message: '不好使',
          code: 201,
        })
      }
    })
  })
//修改用户名
r.post('/name',(req,res)=>{
  pool.query('update user_information set wname=? where wid=?',[
    req.body.wname,
    req.body.wid
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({
        code:200,
        message:"好使"
      })
    }else{
      res.send({
        message: "不好使",
        code: 201,
      })
    }
  }
  )
})
//拿出存在的卡包
r.post('/get',(req,res)=>{
  pool.query(`select * from card where card_id = ?`,[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"有效",
        result
      })
    }else{
      res.send({
        code:201,
        message:"无效",
        result
      })
    }
  })
})
//增加卡卷数据
r.post(`/post`,(req,res)=>{
  pool.query(`insert into card(card_number,card_id,card_name,card_information,card_price,card_url) value (null,?,?,?,?,?)`,[req.body.id,req.body.name,req.body.information,req.body.price,req.body.url],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"有效",
        result
      })
    }else{
      res.send({
        code:201,
        message:"无效",
        result
      })
    }
  })
})
//使用后销毁卡劵
r.post(`/destroy`,(req,res)=>{
  pool.query(`delete from card where card_number = ?`,[req.body.number],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({
        code:200,
        message:"好使"
      })
    }else{
      res.send({
        message: "不好使",
        code: 201,
      })
    }
  })
})
//将订单数据插入到数据库中
r.post(`/insert`,(req,res)=>{
  pool.query(`insert into purchase_information(purchase_id,home_id,home_name,purchase_number,purchase_request,purchase_time,purchase_zone,purchase_url,purchase_price) value (?,?,?,?,?,?,?,?,?)`,[req.body.id,req.body.home_id,req.body.name,req.body.number,req.body.request,req.body.time,req.body.zone,req.body.url,req.body.price],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({
        code:200,
        message:"好使"
      })
    }else{
      res.send({
        message: "不好使",
        code: 201,
      })
    }
  })
})
//订单查询
r.post(`/select1`,(req,res)=>{
  pool.query(`select * from purchase_information where purchase_id =?`,[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//删除订单
r.post('/delete1',(req,res)=>{
  pool.query('delete from purchase_information where purchase_id=? and home_id=?',[req.body.id,req.body.home_id],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//插入入住模块
r.post(`/insert2`,(req,res)=>{
  pool.query(`insert into stay_home(
    person_id,
    home_id,
    home_name,
    purchase_number,
    purchase_request,
    purchase_time,
    purchase_zone,
    purchase_url,
    purchase_price
    ) value (?,?,?,?,?,?,?,?,?)`,[
    req.body.id,
    req.body.home_id,
    req.body.name,
    req.body.number,
    req.body.request,
    req.body.time,
    req.body.zone,
    req.body.url,
    req.body.price
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({
        code:200,
        message:"好使"
      })
    }else{
      res.send({
        message: "不好使",
        code: 201,
      })
    }
  })
})
//住房查询
r.post(`/select2`,(req,res)=>{
  pool.query(`select * from stay_home where person_id =?`,[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//插入评论
r.post(`/insert3`,(req,res)=>{
  pool.query(`insert into review_home(
    review_id,
    home_id,
    home_name,
    purchase_number,
    purchase_request,
    purchase_time,
    purchase_zone,
    purchase_url,
    purchase_price
    ) value (?,?,?,?,?,?,?,?,?)`,[
    req.body.id,
    req.body.home_id,
    req.body.name,
    req.body.number,
    req.body.request,
    req.body.time,
    req.body.zone,
    req.body.url,
    req.body.price
  ],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({
        code:200,
        message:"好使"
      })
    }else{
      res.send({
        message: "不好使",
        code: 201,
      })
    }
  })
})
//评论查询
r.post(`/select3`,(req,res)=>{
  pool.query(`select * from review_home where review_id =?`,[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//评论查询1
r.post(`/select4`,(req,res)=>{
  pool.query(`select * from review_home where review_id =?`,[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//删除入住
r.post('/delete2',(req,res)=>{
  pool.query('delete from stay_home where person_id=? and home_id=?',[req.body.id,req.body.home_id],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})


//数据变活/select 
r.post('/h11',(req,res)=>{
  pool.query('select * from hotel where city_id = ?',[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})
//数据变活/hotel
// r.post('/h12',(req,res)=>{
//   pool.query('select * from roomtype where hotel_id = ?',[req.body.id],(err,result)=>{
//     pool.query('select * from bedtype where roomtype_id = ?',[result.data.result.[]])

//     if(err) throw err;
//     if(result.length){
//       res.send({
//         code:200,
//         message:"好使",
//         result:result
//       })
//     }else{
//       res.send({
//         code: 201,
//         message: "不好使",
//         result:result
//       })
//     }
//   })
// })
// 查询酒店页面的数据
r.post("/hotelzhu", (req, res) => {
  let {id} = req.body;
  let sql = "select * from roomtype where hotel_id = ?";
  let sql1 = "select * from bedtype where bedtype.roomtype_id = ?";

  pool.query(sql,[id] ,(err, result) => {
    let seachbed = function (i) {
      return new Promise((resolve, reject) => {
        pool.query(sql1, [result[i].id], (err, result1) => {
          if (err) throw err;
          result[i].bedtype = result1;
          resolve();
        });
      });
    };
    
    let loop = function() {
      return new Promise(async (resolve,reject)=>{
        for (let i = 0; i < result.length; i++) {
           await seachbed(i);
        }
        resolve()
      })
    }

    if (err) throw err;
    if (result.length == 0) {
      res.send({
        msg: "获取房间信息失败",
        status: 201,
      });
    } else {
      loop().then(()=>{
        res.send({
          msg: "获取房间信息成功",
          status: 200,
          data: result,
        });
       })
    }
  });
});
// hotelroom的头尾信息
r.post('/h12',(req,res)=>{
  pool.query('select * from hotel where id = ?',[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
});
// 轮播图的sql
r.post('/h13',(req,res)=>{
  pool.query('select * from hotelpic where hotel_id = ?',[req.body.id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      res.send({
        code:200,
        message:"好使",
        result:result
      })
    }else{
      res.send({
        code: 201,
        message: "不好使",
        result:result
      })
    }
  })
})

r.post("/hotelpic", (req, res) => {
  let {id} = req.body;
  let sql = "select * from picture_head where hotel_id = ?";
  let sql1 = "select * from picture where picture.details_id = ?";

  pool.query(sql,[id] ,(err, result) => {
    let seachbed = function (i) {
      return new Promise((resolve, reject) => {
        pool.query(sql1, [result[i].id], (err, result1) => {
          if (err) throw err;
          result[i].picture = result1;
          resolve();
        });
      });
    };
    
    let loop = function() {
      return new Promise(async (resolve,reject)=>{
        for (let i = 0; i < result.length; i++) {
           await seachbed(i);
        }
        resolve()
      })
    }

    if (err) throw err;
    if (result.length == 0) {
      res.send({
        msg: "获取房间信息失败",
        status: 201,
      });
    } else {
      loop().then(()=>{
        res.send({
          msg: "获取房间信息成功",
          status: 200,
          data: result,
        });
       })
    }
  });
});


module.exports = r;
